package org.esky.file;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import org.apache.log4j.Logger;

public class OpFile {
	
	private static Logger logger = Logger.getLogger(OpFile.class);
	
	// 创建文件夹
	public static void createDir(String path) throws IOException {
		File folder = new File(path);
		if (!folder.exists()) {
			boolean b = folder.mkdir();
			if (b) {
				logger.info(folder.getPath() + " create success");
			}else {
				logger.info(folder.getPath() + " create fail");
			}
		} else {
			logger.error(folder.getPath() + " already exists");
		}
	}
	
	// 创建多个文件夹
	public static void createDirs(String path) throws IOException {
		File folder = new File(path);
		if (!folder.exists()) {
			if (folder.mkdirs()) {
				logger.info(folder.getPath() + " create success");
			}else {
				logger.info(folder.getPath() + " create fail");
			}
		} else {
			logger.error(folder.getPath() + " already exists");
		}
	}
	
	// 创建文件[内容为空的文件(大小0KB)]
	public static void createFile(String path) throws IOException {
		File file = new File(path);
		if (!file.exists()) {
			if (file.createNewFile()) {
				logger.info(file.getPath() + " create success");
			}else {
				logger.info(file.getPath() + " create fail");
			}
		} else {
			logger.error(file.getPath() + " already exists");
		}
	}
	
	// 删除文件
	public static void deleteFile(String path) throws IOException {
		File file = new File(path);
		if (file.exists()) {
			if (file.isFile()) {
				if (file.delete()) {
					logger.info(file.getPath() + " delete success");
				}else {
					logger.info(file.getPath() + " delete fail");
				}
			}
		} else {
			logger.error(file.getPath() + " not exists");
		}
	}
	
	// 删除文件夹[只能删当前目录，且目录为空]
	public static void deleteDir(String path) throws IOException {
		File folder = new File(path);
		if (folder.exists()) {
			if (folder.isDirectory()) {
				if (folder.delete()) {
					logger.info(folder.getPath() + " delete success");
				}else {
					logger.error(folder.getPath() + " delete fail");
				}
			}
		} else {
			logger.error(folder.getPath() + " not exists");
		}
	}

	// 删除多个文件夹[只能删除当前目录下的所有的空目录]
	public static void deleteDirs(String path) throws IOException {
		File folder = new File(path);
		if (folder.exists()) {
			File[] folders = folder.listFiles();
			for (File file : folders) {
				if (file.isDirectory()) {
					if (file.delete()) {
						logger.info(file.getPath() + " delete success");
					}else {
						logger.error(file.getPath() + " delete fail");
					}
				}
			}
		} else {
			logger.error(folder.getPath() + " not exists");
		}
	}
	
	// 删除当前目录下所有的子目录和文件
	public static void delete(String path) throws IOException {
		File file = new File(path);
		if (!file.exists()) {
			logger.error(file.getPath() + " not exists");
		}
		File files[] = file.listFiles();
		for (int i = 0; i < files.length; i++) {
			if (files[i].isDirectory()) {
				delete(files[i].getPath());
			} else {
				if (!files[i].delete()) {
					logger.error(files[i].getPath() + " delete fail");
				}else {
					logger.info(files[i].getPath() + " delete success");
				}
			}
		}
		if (!file.delete()) {
			// 删除当前目录
			logger.error(file.getPath() + " delete fail");
		} else {
			logger.info(file.getPath() + " delete success");
		}
	}
	
	// FileOutputStream写文件
	public static void write1(String path, String text) throws IOException{
		int count = 100000;// 写文件行数
		long begin = 0l;
		long end = 0l;
		
		FileOutputStream fos = new FileOutputStream(path);
		begin = System.currentTimeMillis(); 
		for (int i = 0; i < count; i++) {
			fos.write(text.getBytes("gb2312"));
		}
		fos.close();
		end = System.currentTimeMillis();   
		logger.info("FileOutputStream 执行耗时:" + (end - begin) + " 豪秒");
	}
	
	// BufferedWriter写文件
	public static void write2(String path, String text) throws IOException{
		int count = 100000;// 写文件行数
		long begin = 0l;
		long end = 0l;
 
		FileOutputStream fos = new FileOutputStream(path);
		OutputStreamWriter osw = new OutputStreamWriter(fos,"gb2312");//gb2312
		BufferedWriter bw = new BufferedWriter(osw);  
		begin = System.currentTimeMillis(); 
		for (int i = 0; i < count; i++) {
			bw.write(text);
		}
		bw.close();
		osw.close();
		end = System.currentTimeMillis();   
		logger.info("BufferedWriter 执行耗时:" + (end - begin) + " 豪秒");
	}
	
	// BufferedOutputStream写文件
	public static void write3(String path, String text) throws IOException{
		int count = 100000;// 写文件行数
		long begin = 0l;
		long end = 0l;
		
		FileOutputStream fos = new FileOutputStream(path);
		BufferedOutputStream bos = new BufferedOutputStream(fos);
		begin = System.currentTimeMillis(); 
		for (int i = 0; i < count; i++) {
			bos.write(text.getBytes("gb2312"));
		}
		bos.close();
		fos.close();
		end = System.currentTimeMillis();   
		logger.info("BufferedOutputStream 执行耗时:" + (end - begin) + " 豪秒");
	}
	
	// FileWriter写文件
	public static void write4(String path, String text) throws IOException{
		int count=100000;//写文件行数   
		long begin = 0l;
		long end = 0l;
		
		FileWriter fw = new FileWriter(path);
		begin = System.currentTimeMillis(); 
		for (int i = 0; i < count; i++) {
			fw.write(text);
		}
		fw.close();
		end = System.currentTimeMillis();   
		logger.info("FileWriter 执行耗时:" + (end - begin) + " 豪秒");
	}
	
	// 读取单个文件
	public static void read(String path) throws IOException{
		File file = new File(path);
//		FileReader fr = new FileReader(file);
		// 可以解决编码问题
		InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "gb2312");
		BufferedReader br = new BufferedReader(isr);
		String line = null;
		while ((line = br.readLine()) != null) {  
			logger.info(line);
		}
		br.close();
		isr.close();
//		fr.close();
	}
}
